package io

import (
	"../constants"
	"../arrays"
)

type Parser interface {
	Parse(cards []Card) []CardGroup
	walkAllCards()
	matches()
}

type CardParser struct {
}

var reducers []Reducer

func (p *CardParser) Parse(cards []Card) []CardGroup {
	out := []CardGroup{}
	remainCards := cards[:]
	for _, v := range reducers {
		for {
			outCards := v.Reduce(remainCards)
			if len(outCards) > 0 {
				g := NewCardGroup()
				g.Pattern = v.Pattern()
				g.Cards = outCards
				out = append(out, *g)

				//arrays.RemoveAll(remainCards, outCards)
				for _, outCard := range outCards  {
					for i, c := range remainCards {
						if outCard == c {
							remainCards = append(remainCards[0 : i], remainCards[i + 1:]...)
							break
						}
					}
				}
			} else {
				break
			}
		}
	}
	return out
}

func NewCardParser() *CardParser {
	return new(CardParser)
}

func init() {
	reducers = []Reducer {
		RocketReducer{CardReducer{constants.PATTERN_ROCKET}},
		BombReducer{CardReducer{constants.PATTERN_BOMB}},
	}
}